<?php defined('SYSPATH') or die('No direct script access.');
class Model_tag
{
	private static $tag_article_count = array();
	
    public function tag2anchor($a){
        $search = $tags = $replace = array();
        foreach ($this->get_tags($a['_id']) as $k=>$tag){
            $search[] = $tag['tag'];
            $tags[] = '{$'.(string)$tag['_id'].'}';
            $replace[] = HTML::anchor(project::tag_url($tag['tag']),$tag['tag']);
        }
        //var_dump($search,$tags,$replace);
        $a['content'] = preg_replace('/<\s?a[^>]*>(.*?)<\s?\/a\s?>/iu', '\1', $a['content']);
        $a['content'] = str_ireplace($search,$tags,$a['content']);
        $a['content'] = str_replace($tags,$replace,$a['content']);
        return $a;
    }
    
    public function get_articles($tag_id){
        $articles = array();
        foreach (MON::factory('tag_article')->find(array('tag_id'=>new MongoId((string)$tag_id))) as $tag_article){
            $articles[] = new MongoId($tag_article['article_id']);
        }
        return MON::factory('article')->find(array('_id'=>array('$in'=>$articles)));
    }
    
    public function get_tags($article_id = null,$limit = null){
        if ($article_id === null){
        	$tags = array();
        	$tag_article_count = &self::$tag_article_count;
        	foreach (MON::factory('tag')->find() as $tag){
        		if ($count = MON::factory('tag_article')->find(array('tag_id'=>$tag['_id']))->count()){
        			$tag_article_count[$tag['tag']] = $count;
        			$tags[] = $tag;
        		}
        	}
        	//var_dump($tag_article_count,$tags);
        	usort($tags,array($this,'cmp'));//按关联文章的数量排序
        	if (is_int($limit))
        		return array_slice($tags,0,$limit);
        	else
        		return $tags;
        }
        
        $tags = array();
        foreach (MON::factory('tag_article')->find(array('article_id'=>new MongoId((string)$article_id))) as $tag_article){
            $tags[] = new MongoId($tag_article['tag_id']);
        }
        return MON::factory('tag')->find(array('_id'=>array('$in'=>$tags)));
    }
    function cmp($a, $b){//按关联文章的数量排序
    	$tag_article_count = &self::$tag_article_count;
    	if ($tag_article_count[$a['tag']] == $tag_article_count[$b['tag']])
    		return 0;
    	return ($tag_article_count[$a['tag']] > $tag_article_count[$b['tag']]) ? -1 : 1;
    }
    
    public function get_tags_text($article_id,$glue = ',',$limit = null){
    	$tags = MON::as_array($this->get_tags($article_id,$limit));
    	$tags_text = common::array_deep_implode($glue,$tags,array('tag'));
    	return $tags_text;
    }
    
    public function get_tags_anchor($article_id,$glue = ',',array $attributes = NULL,$limit = null){
    	$tags = MON::as_array($this->get_tags($article_id,$limit));
    	$pieces = array();
    	foreach ($tags as $tag){
    		$pieces[] = HTML::anchor(project::tag_url($tag['tag']),$tag['tag'],$attributes);
    	}
    	return implode($glue, $pieces);
    }
    
    public function tag_batch(){
    	$tags = MON::factory('tag')->find();
    	$articles = MON::factory('article')->find();
    	
    	MON::factory('tag_article')->remove();
    	foreach ($tags as $tag){
    		$tag_id = $tag['_id'];
    		$tag = $tag['tag'];
    		echo 'tag:'.$tag."<br />";
    		
    		$an = 0;
    		foreach ($articles as $article){
    			if ((stripos($article['title'], $tag) === false) &&
    			(stripos($article['keywords'], $tag) === false) &&
    			(stripos($article['content'], $tag) === false)
    			) continue;
    			MON::factory('tag_article')->insert(array(
						'article_id'=>new MongoId($article['_id']),
						'tag_id'=>new MongoId($tag_id)
					));
    			echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;article ".++$an.':';
    			echo $article['title'];
    			echo '<br />';
    		}
    	}
    }
    
    public function tag_born($article_id){
    	$article_id = (string)$article_id;
    	$tags = MON::factory('tag')->find();
    	$article = MON::factory('article',$article_id);
    	
    	MON::factory('tag_article')->remove(array('article_id'=>new MongoId($article_id)));
    	$tn = 0;
    	echo 'article:'.$article['title']."<br />";
    	foreach ($tags as $tag){
    		$tag_id = $tag['_id'];
    		$tag = $tag['tag'];    		
    		
    		if ((stripos($article['title'], $tag) === false) &&
    		(stripos($article['keywords'], $tag) === false) &&
    		(stripos($article['content'], $tag) === false)
    		) continue;
    		MON::factory('tag_article')->insert(array(
					'article_id'=>new MongoId($article['_id']),
					'tag_id'=>new MongoId($tag_id)
				));
    		echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tag ".++$tn.':';
    		echo $tag;
    		echo '<br />';
    	}
    }
}
